package com.dotmedia.dboperations;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.dotmedia.core.DatabaseAccessService;
import com.dotmedia.entities.PermissionGroup;

/**
 * This class is the class which does all the database operations for the entity PermissionGroup.
 * 
 * @author siddharthsaha
 * 
 */
public class PermissionGroupDatabaseOperations {
	private static Logger logger = Logger.getLogger("PermissionGroupDatabaseOperations");

	/**
	 * This method will persist an entity of the PermissionGroup type
	 * 
	 * @param entity
	 *        The entity of PermissionGroup which needs to be persisted into the database
	 */
	public void persistPermissionGroupEntity(PermissionGroup permissionGroupEntity) {
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		logger.info("Persisting an entity of PermissionGroup");
		int persistReturnStatus = databaseAccessService.persistEntity(permissionGroupEntity);
		if (persistReturnStatus == 0) {
			logger.info("The entity of PermissionGroup has been successfully persisted");
		} else {
			logger.error("The entity of PermissionGroup was not successfully persisted");
		}
	}

	/**
	 * This method will update an entity of permission group if that entity is already present in the database.
	 * 
	 * @param permissionGroupEntity
	 *        The PermissionGroup entity which has to be updated
	 */
	public void updatePermissionGroupEntity(PermissionGroup permissionGroupEntity) {
		String query = "SELECT permissionGroupEntity FROM PermissionGroup permissionGroupEntity where permissionGroupEntity.id = "
				+ permissionGroupEntity.getId();
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retrievedEntities = databaseAccessService.getEntities(query);
		boolean updated = false;
		for (int retrievedEntitiesListIndex = 0; retrievedEntitiesListIndex < retrievedEntities.size(); retrievedEntitiesListIndex++) {
			PermissionGroup currentPermissionGroupEntity = (PermissionGroup) retrievedEntities.get(retrievedEntitiesListIndex);
			if (currentPermissionGroupEntity.getId().intValue() == permissionGroupEntity.getId().intValue()) {
				// this is it - update it
				logger.info("Updating the entity of PermissionGroup");
				currentPermissionGroupEntity.setDescription(permissionGroupEntity.getDescription());
				int updateReturnStatus = databaseAccessService.updateEntity(permissionGroupEntity);
				if (updateReturnStatus == 0) {
					logger.info("PermissionGroup entity updated");
					updated = true;
				} else {
					logger.info("PermissionGroup entity cannot be updated");
				}
				break;
			}
		}
		if (!updated) {
			logger.warn("PermissionGroup entity was not updated. Not obtained");
		}
	}

	/**
	 * This method removes an entity of PermissionGroup from the database
	 * 
	 * @param permissionGroupEntity
	 *        The PermissionGroup entity to be removed
	 */
	public void removePermisionGroupEntity(PermissionGroup permissionGroupEntity) {
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		logger.info("Removing PermissionGroup Entity with id = " + permissionGroupEntity.getId());
		int removeReturnStatus = databaseAccessService.removeEntity(permissionGroupEntity, permissionGroupEntity.getId().intValue());
		if (removeReturnStatus == 0) {
			logger.info("PermissionGroup Entity with id = " + permissionGroupEntity.getId() + " was deleted from database");
		} else {
			logger.error("PermissionGroup Entity with id = " + permissionGroupEntity.getId() + " was not deleted from the database");
		}
	}

	/**
	 * This method gets all the permission group entities in the system
	 * 
	 * @return A list of all the permission group entities
	 */
	public List<PermissionGroup> getAllPermissionGroupEntities() {
		logger.info("IN method getAllPermissionGroupEntities() in PermissionGroupDatabaseOperations");
		List<PermissionGroup> toReturnList = new ArrayList<PermissionGroup>();
		String query = "SELECT permissionGroupEntity FROM PermissionGroup permissionGroupEntity";
		logger.info("Query: " + query);
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List returnedEntities = databaseAccessService.getEntities(query);
		for (int returnedEntitiesIndex = 0; returnedEntitiesIndex < returnedEntities.size(); returnedEntitiesIndex++) {
			PermissionGroup currentEntity = (PermissionGroup) returnedEntities.get(returnedEntitiesIndex);
			toReturnList.add(currentEntity);
		}
		logger.info("OUT method getAllPermisisonGroupEntities() in PermissionGroupDatabaseOperations");
		return toReturnList;
	}

}
